package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * cskaoyan
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;


    @Override
    public OrderListResponse queryOrder(OrderListRequest orderListRequest) {
        //分页
        PageHelper.startPage(orderListRequest.getPage(), orderListRequest.getSize());

        OrderListResponse response = new OrderListResponse();
        List<OrderDetailInfo> detailInfoList = new ArrayList<>();
        //获取订单信息
        Order order = new Order();
        order.setUserId(orderListRequest.getUserId());
        List<Order> orderList = orderMapper.select(order);
        if (CollectionUtils.isEmpty(orderList)) {
            // TODO: 2021/12/16 没有订单的情况
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg("没有数据");
            return response;
        }
        //通过订单号去查找 detailInfoList 中的订单商品表和运输表
        for (Order order1 : orderList) {
            OrderDetailInfo detailInfo = new OrderDetailInfo();
            //将orderList的信息注入到detailInfo中
            detailInfo.setOrderId(order1.getOrderId());
            detailInfo.setPayment(order1.getPayment());
            detailInfo.setPaymentType(order1.getPaymentType());
            detailInfo.setPostFee(order1.getPostFee());
            detailInfo.setStatus(order1.getStatus());
            detailInfo.setCreateTime(order1.getCreateTime());
            detailInfo.setUpdateTime(order1.getUpdateTime());
            detailInfo.setPaymentTime(order1.getPaymentTime());
            detailInfo.setConsignTime(order1.getConsignTime());
            detailInfo.setEndTime(order1.getEndTime());
            detailInfo.setCloseTime(order1.getCloseTime());
            detailInfo.setShippingName(order1.getShippingName());
            detailInfo.setShippingCode(order1.getShippingCode());
            detailInfo.setUserId(order1.getUserId());
            detailInfo.setBuyerMessage(order1.getBuyerMessage());
            detailInfo.setBuyerNick(order1.getBuyerNick());
            detailInfo.setBuyerComment(order1.getBuyerComment());

            // 获取 detailInfoList中的List<OrderItemDto> orderItemDtos
            String orderId = order1.getOrderId();
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(orderId);
            List<OrderItem> orderItems1 = orderItemMapper.select(orderItem);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems1);
            detailInfo.setOrderItemDto(orderItemDtos);
            //获取detailInfoList中的OrderShippingDto
            OrderShipping orderShipping = new OrderShipping();
            orderShipping.setOrderId(orderId);
            OrderShipping orderShippings = orderShippingMapper.selectOne(orderShipping);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShippings);
            detailInfo.setOrderShippingDto(orderShippingDto);
            detailInfoList.add(detailInfo);
        }
        PageInfo info = new PageInfo(detailInfoList);
        long total = info.getTotal();
        response.setDetailInfoList(detailInfoList);
        response.setTotal(total);
        // TODO: 2021/12/16 成功逻辑
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }

    @Override
    public OrderDetailDtoResponse queryOrderDetail(OrderDetailRequest request, Long uid, String username) {
        String orderId = request.getOrderId();
        OrderDetailDtoResponse orderDetailDtoResponse = new OrderDetailDtoResponse();
        OrderDetailDto result = new OrderDetailDto();
        Order order = new Order();
        order.setOrderId(orderId);
        /**
         * 根据订单id查询，查询到一个订单
         */
        List<Order> orderList = orderMapper.select(order);
        Order orderAns = orderList.get(0);
        Long userId = orderAns.getUserId();
        result.setOrderStatus(orderAns.getStatus());
        result.setOrderTotal(orderAns.getPayment());


        /**
         * 查询邮寄信息
         */
        OrderShipping orderShipping = new OrderShipping();
        orderShipping.setOrderId(orderId);
        List<OrderShipping> orderShippings = orderShippingMapper.select(orderShipping);
        OrderShipping orderShippingAns = orderShippings.get(0);
        result.setStreetName(orderShippingAns.getReceiverAddress());
        result.setTel(orderShippingAns.getReceiverPhone());

        /**
         * 设置用户信息
         */
        result.setUserId(uid.intValue());
        result.setUserName(username);

        /**
         * 根据订单id查询order-item
         */
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(orderId);
        List<OrderItem> orderItemList = orderItemMapper.select(orderItem);
        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItemList);
        result.setGoodsList(orderItemDtos);
        orderDetailDtoResponse.setResult(result);
        orderDetailDtoResponse.setCode("000000");
        orderDetailDtoResponse.setMsg("成功");
        return orderDetailDtoResponse;
    }
}
